package Math;

import java.util.Stack;

public class _856_ScoreofParentheses {
    public int scoreOfParentheses(String S) {
        Stack<Integer> nums = new Stack<>();
        nums.push(0);
        for (char c : S.toCharArray()) {
            if (c == '(') {
                nums.push(0);
            } else {
                int pre = nums.pop();
                int repre = nums.pop();
                nums.push(repre + Math.max(2 * pre, 1));
            }
        }
        return nums.pop();
    }
}
